import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/home/index.vue'
// 主页

import Order from '../views/order/Order.vue'
// 订单

import Shopcar from '../views/shopcar/Shopcar.vue'
// 购物车

// 登录
import Landing from '../views/Login/Landing.vue'

// 我的 
import Miner from '../views/Miner/Miner.vue'
import MyMasg from '../views/Miner/MyMasg.vue'

//地址
import Address from '../views/address/Address.vue'

// 新建地址
import NewAddress from '../views/address/Newaddress.vue'


// 注册
import Register from '../views/Login/Registered.vue'

// 搜索
import Search from '../views/Serach/Search.vue'

// 商铺详情
import Details from '../views/Shopname/Details.vue'
import ShopnameList from '../views/Shopname/ShopnameList.vue'

// 确认订单
import Ordergoods from '../views/Ordergoods/Ordergoods.vue'
import New from '../views/address/New.vue'

const routes = [
    // 首页
    {
        path: '/',
        name: 'home',
        component: HomeView,
        meta: {
            auth: true
        }
    },

    //订单
    {
        path: '/Order',
        name: 'Order',
        component: Order,
        meta: {
            auth: true
        }

    },

    // 购物车
    {
        path: '/Shopcar',
        name: 'Shopcar',
        component: Shopcar,
        meta: {
            auth: true
        }
    },

    // 我的页面
    {
        path: '/Miner',
        name: 'Miner',
        component: Miner,
        meta: {
            auth: true
        }
    },

    // 我的地址
    {
        path: '/Address',
        name: 'Address',
        component: Address
    },

    // 新建地址
    {
        path: '/NewAddress',
        name: 'NewAddress',
        component: NewAddress
    },
    // 登录
    {
        path: '/Landing',
        name: 'Landing',
        component: Landing
    },
    // 注册
    {
        path: '/Register',
        name: 'Register',
        component: Register
    },
    // 搜索
    {
        path: '/Search',
        name: 'Search',
        component: Search
    },

    // 商铺详情
    {
        path: '/Details',
        name: 'Details',
        component: Details
    },

    {
        path: '/ShopnameList',
        name: 'ShopnameList',
        component: ShopnameList
    },


    // 确认详情
    {
        path: '/Ordergoods',
        name: 'Ordergoods',
        component: Ordergoods
    },

    {
        path: '/New',
        name: 'New',
        component: New
    },

    {
        path: '/MyMasg',
        name: 'MyMasg',
        component: MyMasg
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

router.beforeEach((to, from, next) => {

    if (to.meta.auth) {
        let token = sessionStorage.getItem('token')

        if (!token) {
            router.push("/Landing?redirect=" + to.fullPath)
        } else {
            next()
        }
    } else {
        next()
    }
})
export default router
